//获取应用实例
const app = getApp();
const util = require('../../utils/util')
Page({
  data: {
    type: 0,
    num: 1,
    pice: 10,
    payid: '',
    couponPrice: 0,//优惠券金额
    all_pice: 10,
    rmb: 10,
    printType: '0',
    title: '复印',
    ccinfo: 'A4',
    facilityCode: '',
    pics: [],
    couponTitle: '请选择优惠券',
    couponId: '',//优惠券ID
    isColours: true,
    typeCode: 12,
    pictow: '',
    inputPassword: '', //输入的密码
    passwordInputHidden: true, //hidden是true 默认隐藏
    pay_type: 1, //支付方式
    select_fs: [{
      name: '0',
      value: '单色打印'
    },
    {
      name: '1',
      value: '彩色打印',
      checked: 'true'
    }
    ],
    select_f: [
      {
        name: '1',
        value: '微信支付',
        checked: 'true'

      }, {
        name: '0',
        value: '余额支付',
      }
    ],
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '提交订单', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  loadPrice: function (success) {
    let that = this;
    let url = app.globalData.URL + 'facility/print/price/facility/price'
    let data = {
      count: this.data.num,
      isColours: this.data.isColours,
      isDoubleFaced: false,
      type: this.data.typeCode,
      facilityCode: this.data.facilityCode
    }
    wx.request({
      url: url,
      method: 'GET',
      header: {
        'Authorization': getApp().globalData.token // 默认值
      },
      data: data,
      success: function (res) {
        let params = {
          pice: res.data.data.price,
          all_pice: parseFloat(res.data.data.price * that.data.num).toFixed(2),
        }
        console.log(params);
        that.setData(params)
        if (success) success.call(this);
      }
    })
  },
  onLoad(options) {
    console.log(options.url);
    let that = this;
    let pics = options.url;
    let facilityCode = app.globalData.jqm;
    that.setData({ facilityCode: facilityCode, pics: pics, title: this.getTitle(options.prType) + "复印" })
    that.loadPrice(facilityCode);
  },
  getTitle: function (prType) {
    return prType == 2 ? '身份证' : prType == 3 ? '户口本' : prType == 4 ? '营业执照' : '驾驶证或行驶证';
  },
  // 添加打印机
  gosaomiao() {
    let that = this;
    wx.scanCode({
      success(res) {
        let facilityCode = res.result;
        that.setData({
          facilityCode: facilityCode
        })
        wx.showModal({
          title: '设备添加成功',
          icon: 'none'
        })
      }
    })
  },
  inputPassword(e) {
    let that = this;
    //键盘输入的密码 赋值给inputPassword
    let inputPassword = that.data.inputPassword + e.currentTarget.dataset.key;
    that.setData({
      inputPassword: inputPassword
    })
    //当输入密码完成时 提交密码并给个提示
    if (that.data.inputPassword.length == 6) {
      wx.showLoading({
        title: '支付中',
      })
      let url = app.globalData.URL + 'facility/printOrder/pay?id=' + that.data.payid + '&payPassword=' + that.data.inputPassword;
      let data = {}
      app.wxRequest('POST', url, data, (res1) => {
        wx.hideLoading();
        if (res1.code == 200) {
          // 支付成功
          console.log(res1)
          that.passwordInputHidden();
          wx.showModal({
            title: '支付成功',
            icon: 'none'
          })
          wx.switchTab({
            url: '/pages/index/index'
          });
        } else if (res1.code == 500) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "余额不足",
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 502) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "输入密码错误",
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 501) {
          // 未设置支付密码
          that.passwordInputHidden()
          wx.showModal({
            title: "提示",
            content: "设置支付密码",
            success() {
              wx.navigateTo({
                url: '/pages/my/setPassword/setPassword'
              })
            }
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
    }

  },
  passwordInputHidden() {
    this.setData({
      passwordInputHidden: !this.data.passwordInputHidden //取反 打开关闭小键盘
    });
    this.setData({
      inputPassword: ''
    });
  },
  //删除输入错误的密码
  clear() {
    var index = this.data.inputPassword.length;
    if (index > 0) {
      var inputPassword = this.data.inputPassword.substr(0, index - 1);
      this.setData({
        inputPassword: inputPassword
      });
    }
  },
  mycoupon: function () {
    wx.navigateTo({
      url: '/modelB/pages/my/my_coupon/my_coupon',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  select_dayin: function (e) {
    let printType = e.detail.value;
    this.setData({
      printType: printType
    })
  },
  select_zhifu: function (e) {
    let pay_type = e.detail.value;
    this.setData({
      pay_type: pay_type,
    })
    console.log(this.data.pay_type)
  },
  // 计算价格
  calculateTotal: function () {
    let that = this;
    let all_pice = ((that.data.pice * that.data.num) * 100 - that.data.couponPrice * 100) / 100;
    if (all_pice > 0) {
      console.log(all_pice)
      that.setData({
        all_pice: parseFloat(all_pice).toFixed(2),
      })
    } else {
      console.log(all_pice)
      that.setData({
        all_pice: 0
      })
    }
  },
  // 减一
  minusCount: function (e) {
    var num = this.data.num;
    // 商品总数量-1
    if (num > 1) {
      num--;
    }
    // 将数值与状态写回  
    this.setData({
      num: num,
    });
    this.calculateTotal()
    console.log(this.data.num)
    // getCount()
  },
  /* 加数 */
  addCount: function (e) {
    var num = this.data.num;
    // 总数量-1  
    if (num < 1000) {
      num++;
    }
    // 将数值与状态写回  
    this.setData({
      num: num,
    });
    this.calculateTotal()
    // getCount()
  },
  // 预览
  gotophoto: function () {
    let pic = this.data.pics;
    wx.navigateTo({
      url: '/modelA/pages/drawings_printing/picture_editing/picture_editing?pic=' + pic + '&ccinfo=' + this.data.ccinfo + '&title=' + this.data.title + '&type=' + this.data.type + '&isColours=' + this.data.isColours + '&id=' + this.data.typeCode + "&pagesType=FY",
    })
  },
  // 提交订单
  submitpho: function () {
    let that = this;
    if (that.data.facilityCode.indexOf("@") == -1) {
      wx.showModal({
        title: '请添加机器',
        icon: 'none',
        success() {
          that.gosaomiao();
        }
      })
    } else {
      wx.showLoading({
        title: '提交中，请耐心等待^_^',
      })
      // 上传到服务器
      if (that.data.isUpload) {
        that.createOrder();
      } else {
        console.log(that.data.pics);
        util.uploadImage(that.data.pics, getApp().globalData.token, (data) => {
          that.setData({
            pics: data.url,
            isUpload: true
          })
          that.createOrder();
        })
      }
    }
  },
  createOrder: function () {
    const that = this;
    //创建订单
    let url = app.globalData.URL + 'facility/printOrder?';
    wx.request({
      url: url + 'facilityCode=' + that.data.facilityCode + '&content=' + that.data.pics + '&count=' + that.data.num + '&isColours=' + that.data.isColours + '&printType=' + that.data.printType + '&title=' + that.data.title + '&type=' + that.data.type + '&typeCode=' + that.data.typeCode + '&payType=' + that.data.pay_type + '&couponId=' + that.data.couponId,
      method: 'POST',
      header: {
        'Authorization': getApp().globalData.token // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        if (res.statusCode != 200 || res.data.code != 200) {
          wx.showToast({
            title: res.data ? res.data.msg : '服务调用失败，请稍后再试',
            icon: 'none'
          })
          return false;
        }
        that.setData({
          payid: res.data.data.id
        })
        let pay_type = that.data.pay_type;
        if (pay_type == 0) {
          // 余额支付
          that.passwordInputHidden();
        } else if (pay_type == 1) {
          // 吊起微信支付
          console.log('01')
          let wxzf = res.data.data;
          console.log(wxzf)
          wx.requestPayment({
            timeStamp: wxzf.timeStamp,
            nonceStr: wxzf.nonceStr,
            package: wxzf.package,
            signType: wxzf.signType,
            paySign: wxzf.paySign,
            success(res) {
              console.log('支付成功')
              console.log(res)
              wx.switchTab({
                url: '/pages/index/index',
              })
            },
            fail(res) { }
          })
        }
      }
    })
  }
})